from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SPUSpecification, SPU
from apps.meiduo_admin.serializers.specs import SPUSpecificationSerializer, SPUSerializer
from apps.meiduo_admin.utils import UserPageNum


class SpecsView(ModelViewSet):
    '''
        规格表的增删改查
    '''
    # 指定查询集
    queryset = SPUSpecification.objects.all()
    # 指定序列化器
    serializer_class = SPUSpecificationSerializer
    # 指定分页器
    pagination_class = UserPageNum
    # 指定权限
    permission_classes = [IsAdminUser]

    # 自定义方法获取SPU表数据
    def simple(self, request):
        # 1、查询SPU表获取所有SPU表数据
        spus = SPU.objects.all()
        # 2、序列化返回SPU数据
        s = SPUSerializer(spus, many=True)
        return Response(s.data)